/* eslint-disable */
var CryptoJS = require("crypto-js");
var request = {
  "id": "e0abe94e-7c79-4bc8-bf26-393038ef40cd",
  "name": "http://stapi-eal.gigrt.com/product/v1/relationship/list?offerNo=111",
  "headers": {
    "date": "{{Date}}",
    "content-md5": "{{Md5}}",
    "x-ca-nonce": "{{Nonce}}",
    "x-ca-key": "{{AppKey}}",
    "x-ca-signature": "{{Signature}}",
    "x-ca-signaturemethod": "HmacSHA256",
    "x-ca-signature-headers": "{{SignatureHeaders}}",
    "cache-control": "no-cache",
    "postman-token": "{{$guid}}",
    "content-type": ""
  },
  "method": "GET",
  "url": "http://stapi-eal.gigrt.com/product/v1/relationship/list?offerNo=111",
  "data": {}
};

var appKey = "<YOUR APP KEY>";
var appSecret = "<YOUR APP SECRET>";

var md5 = calcMd5();
var date = new Date().toString();
var nonce = createUuid();

var textToSign = "";

textToSign += request.method + "\n";
textToSign += request.headers["accept"] + "\n";
textToSign += md5 + "\n";
textToSign += request.headers["content-type"] + "\n";
textToSign += date + "\n";

var headers = headersToSign();
var signatureHeaders;
var sortedKeys = Array.from(headers.keys()).sort()
for (var headerName of sortedKeys) {
  textToSign += headerName + ":" + headers.get(headerName) + "\n";
  signatureHeaders = signatureHeaders ? signatureHeaders + "," + headerName : headerName;
}
textToSign += urlToSign();
console.log("textToSign\n" + textToSign.replace(/\n/g, "#"));
var hash = CryptoJS.HmacSHA256(textToSign, appSecret)
console.log("hash:" + hash)
var signature = hash.toString(CryptoJS.enc.Base64)
console.log("signature:" + signature)

// pm.globals.set('AppKey', appKey);
// pm.globals.set('Md5', md5);
// pm.globals.set("Date", date);
// pm.globals.set("Signature", signature);
// pm.globals.set("SignatureHeaders", signatureHeaders);
// pm.globals.set("Nonce", nonce);

function headersToSign() {
  var headers = new Map();
  for (var name in request.headers) {
    name = name.toLowerCase();
    if (!name.startsWith('x-ca-')) {
      continue;
    }
    if (name === "x-ca-signature" || name === "x-ca-signature-headers" || name == "x-ca-key" || name === 'x-ca-nonce') {
      continue;
    }
    var value = request.headers[name];
    headers.set(name, value);
  }
  headers.set('x-ca-key', appKey);
  headers.set('x-ca-nonce', nonce);
  return headers;
}

function urlToSign() {
  var params = new Map();
  var contentType = request.headers["content-type"];
  console.log("contentType", contentType)
  if (contentType && contentType.startsWith('application/x-www-form-urlencoded')) {
    const formParams = request.data.split("&");
    formParams.forEach((p) => {
      const ss = p.split('=');
      params.set(ss[0], ss[1]);
    })
  }

  const ss = request.url.split('?');
  if (ss.length > 1 && ss[1]) {
    const queryParams = ss[1].split('&');
    queryParams.forEach((p) => {
      const ss = p.split('=');
      params.set(ss[0], ss[1]);
    })
  }

  var sortedKeys = Array.from(params.keys())
  sortedKeys.sort();

  var l1 = ss[0].lastIndexOf('/');
  var url = ss[0].substring(l1);
  var first = true;
  var qs
  for (var k of sortedKeys) {
    var s = k + "=" + params.get(k);
    qs = qs ? qs + "&" + s : s;
    console.log("key=" + k + " value=" + params.get(k));
  }
  return qs ? url + "?" + qs : url;
}

function calcMd5() {
  var contentType = request.headers["content-type"];
  if (request.data && !contentType.startsWith('application/x-www-form-urlencoded')) {
    var data = request.data;
    var md5 = CryptoJS.MD5(data);
    var md5String = md5.toString(CryptoJS.enc.Base64);
    console.log("data:" + JSON.stringify(data) + "\nmd5:" + md5String);
    return md5String;
  } else {
    return "";
  }
}

function createUuid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
}
